package com.atguigu.seckill.controller;


import com.atguigu.seckill.pojo.Goods;
import com.atguigu.seckill.pojo.Order;
import com.atguigu.seckill.pojo.SeckillOrder;
import com.atguigu.seckill.pojo.User;
import com.atguigu.seckill.service.IGoodsService;
import com.atguigu.seckill.service.IOrderService;
import com.atguigu.seckill.service.ISeckillGoodsService;
import com.atguigu.seckill.service.ISeckillOrderService;
import com.atguigu.seckill.vo.CodeResponseEnum;
import com.atguigu.seckill.vo.GoodsVo;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.org.apache.bcel.internal.classfile.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sjj
 * @since 2021-04-13
 */
@Controller
@RequestMapping("/seckill")
public class SeckillGoodsController {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private ISeckillOrderService seckillOrderService ;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("/doSeckill")
    public String doSeckill(Long goodsId,User user,Model model){
        model.addAttribute("user",user);
        //首先获取到当前商品的具体信息
        GoodsVo goodsVo=goodsService.getGoodsDeatil(goodsId);
        //对当前商品库存进行判空
        int stock=goodsVo.getGoodsStock();
        if(stock<1){
            model.addAttribute("errmsg", CodeResponseEnum.EMPTY_STOCK);
            return "secKillFail";
        }
        //判断是否属于重复抢购
        SeckillOrder seckillOrder=seckillOrderService.getOne(new QueryWrapper<SeckillOrder>().eq("user_id",user.getId()).eq("goods_id",goodsId));
        if (seckillOrder!=null){
            model.addAttribute("errmsg", CodeResponseEnum.DOUBLE_SECKILL);
            return "secKillFail";
        }
    /*public String doSeckill(Model model,Long goodsId,@CookieValue("userTicket") String ticket){
        //通过redis获取用户信息
        User user=(User)redisTemplate.opsForValue().get("user:"+ticket);
        if(user==null){return "login";}
        model.addAttribute("user",user);
        //首先获取到当前商品的具体信息
        GoodsVo goodsVo=goodsService.getGoodsDeatil(goodsId);
        //对当前商品库存进行判空
        int stock=goodsVo.getGoodsStock();
        if(stock<1){
            model.addAttribute("errmsg", CodeResponseEnum.EMPTY_STOCK);
            return "secKillFail";
        }
        //判断是否属于重复抢购
        SeckillOrder seckillOrder=seckillOrderService.getOne(new QueryWrapper<SeckillOrder>().eq("user_id",user.getId()).eq("goods_id",goodsId));
        if (seckillOrder!=null){
            model.addAttribute("errmsg", CodeResponseEnum.DOUBLE_SECKILL);
            return "secKillFail";
        }*/
        //抢购
        Order order=orderService.secKill(user,goodsVo);
        model.addAttribute("order",order);
        model.addAttribute("goods",goodsVo);
        return "orderDetail";
    }


}
